Imports PTT.CarRes.Data

Public Class ClassQueryInvCarFuel
    Public Function Add(ByVal InvCarFuel As QueryInvCarFuel, ByVal StrConn As String) As Boolean
        Dim ClsInvCarFuel As New ClassInvCarFuel
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarFuel.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvCarFuel
            .FuelId = InvCarFuel.FuelId
            .FuelName = InvCarFuel.FuelName
            .FuelLite = InvCarFuel.FuelLite
            .FuelStatus = InvCarFuel.FuelStatus
            ret = .USP_Save(Conn)
        End With
        ClsInvCarFuel.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Edit(ByVal InvCarFuel As QueryInvCarFuel, ByVal StrConn As String) As Boolean
        Dim ClsInvCarFuel As New ClassInvCarFuel
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarFuel.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvCarFuel
            .FuelId = InvCarFuel.FuelId
            .Load(Conn)
            .FuelName = InvCarFuel.FuelName
            .FuelLite = InvCarFuel.FuelLite
            .FuelStatus = InvCarFuel.FuelStatus
            ret = .USP_Update(Conn)
        End With
        ClsInvCarFuel.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Delete(ByVal InvCarFuel As QueryInvCarFuel, ByVal StrConn As String) As Boolean
        Dim ClsInvCarFuel As New ClassInvCarFuel
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarFuel.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvCarFuel
            .FuelId = InvCarFuel.FuelId
            .Load(Conn)
            ret = .USP_Delete(Conn)
        End With
        ClsInvCarFuel.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Search(Optional ByVal ParaFuelId As String = "", _
    Optional ByVal ParaFuelName As String = "", _
    Optional ByVal ParaFuelLite As String = "", _
    Optional ByVal ParaFuelStatus As String = "", _
    Optional ByVal StrConn As String = "") As QueryInvCarFuels
        Dim ClsInvCarFuel As New ClassInvCarFuel
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarFuel.ConnectionOpen(StrConn)
        Dim ret As QueryInvCarFuels = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvCarFuel"
        If ParaFuelId & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(FuelId = " & ParaFuelId & ")"
        End If
        If ParaFuelName & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(FuelName like '%" & ParaFuelName & "%')"
        End If
        If ParaFuelLite & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(FuelLite like '%" & ParaFuelLite & "%')"
        End If
        If ParaFuelStatus & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(FuelStatus like '%" & ParaFuelStatus & "%')"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        End If
        Dim ds As DataSet
        With ClsInvCarFuel
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvCarFuels

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvCarFuel( _
                              .Item("FuelId") & "", _
                              .Item("FuelName") & "", _
                              .Item("FuelLite") & "", _
                              .Item("FuelStatus") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvCarFuel.ConnectionClose(Conn)
        Return ret
    End Function
    Public Function Load(Optional ByVal ParaFuelId As String = "", Optional ByVal StrConn As String = "") As QueryInvCarFuels
        Dim ClsInvCarFuel As New ClassInvCarFuel
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarFuel.ConnectionOpen(StrConn)
        Dim ret As QueryInvCarFuels = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvCarFuel"
        If ParaFuelId & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(FuelId = " & ParaFuelId & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        Else
            Return Nothing
            Exit Function
        End If
        Dim ds As DataSet
        With ClsInvCarFuel
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvCarFuels

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvCarFuel( _
                              .Item("FuelId") & "", _
                              .Item("FuelName") & "", _
                              .Item("FuelLite") & "", _
                              .Item("FuelStatus") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvCarFuel.ConnectionClose(Conn)
        Return ret
    End Function
End Class